//
//  ProductListViewController.swift
//  SwiftDemo
//
//  Created by cqf on 2025/9/5.
//

import UIKit

struct Item {
    let id:Int
    let title:String
    let intro:String
}

class ProductListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    private let tableView = UITableView()
    private var items:[String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
        setupTableView()
        loadData()
    }
    private func setupView() {
        view.backgroundColor = .white
        title = "TableViewDemo"
    }

    func loadData() {
        self.items = ["eggs","milk"]
        self.tableView.reloadData()
    }
    
    private func setupTableView() {
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellid")
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.backgroundColor = .systemBackground
        tableView.separatorStyle = .singleLine
        tableView.rowHeight = UITableView.automaticDimension
        tableView.frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-120)
        view.addSubview(tableView)
    }
    
    //代理
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath) as? UITableViewCell else {
                   return UITableViewCell()
            }
        cell.textLabel?.text = items[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    //函数
    func test() -> String {
        return "hello "
    }
    
    func testFunModel(peopelid: String) {
        print("Hello, \(peopelid)!")
        
        // 定义模型
        struct User: Codable {
            let id: Int
            let name: String
            let email: String?
        }
        
        let jsonString = """
        {
            "id": 1001,
            "name": "张三",
            "email": "zhangsan@example.com"
        }
        """
        if let jsonData = jsonString.data(using: .utf8) {
           
        }
    }
}
